package com.blensmile.activity;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.blensmile.BaseActivity;
import com.blensmile.R;
import com.blensmile.view.TalkMateLoadingView;

public class AnimMovementTestActivity extends BaseActivity implements View.OnClickListener{

    public static final byte TIME_UPDATE = 0x0f;
    public static final byte LOADING = 0x1f;
    public static final byte LOAD_FAIL = 0x2f;
    public static final byte LOADED = 0x3f;
    public static final byte CLOSE  = 0x44;
    public static final byte HIDE_LOADING = 0x7f;

    //views
    private View llLoading;
    private View llChapion;
    private TextView tvTitle;
    private View icLoading;
    private View llTest;
    private ImageView imgAvatar;
    private TextView tvRank;
    private TextView tvName;
    private TextView tvInfo;
    private TalkMateLoadingView tmvLoading;
    private TextView tvLoading;
    private TextView tvTime;
    private TextView tvTestCnt;
    //variables
    private String partid;
    private String coursecode;
    private int type;
    private String chapterCode;
    private int timeCounter = 0; //计时器
    private byte loadingType = 0;
    private boolean isLoading = true;
    private int slideCnt = 0;
    private int testCnt = 0;
    private boolean hasHistory = true;
    private TextView tvHistory;

    Handler chapterHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case TIME_UPDATE:
                    if(timeCounter<1480) {
                        timeCounter += 20;
                        tvLoading.setText(String.format(getString(R.string.course_loading),String.format("%1$2d%%",timeCounter/15)));
                        tmvLoading.setProgress((byte)(timeCounter/15));
                        sendEmptyMessageDelayed(TIME_UPDATE, 20);
                    }else {
                        getSlideCnt();
                        isLoading = false;
                    }
                    break;
                case LOADING:
                    timeCounter = 0;
                    sendEmptyMessageDelayed(TIME_UPDATE,20);
                    break;
                case LOADED:
                    isLoading = false;
                    if(tvLoading != null) {
                        tvLoading.setText(String.format(getString(R.string.course_loading), String.format("%1$2d%%", 100)));
                        tmvLoading.setProgress((byte) (100));
                    }
                    showTestView();
                    break;
                case CLOSE:
                    finish();
                    break;
                case HIDE_LOADING:
                    llLoading.setVisibility(View.GONE);
                    break;
                case 1000:
                    getLearnMates();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        chapterCode = getIntent().getStringExtra("chapterCode");
        partid = getIntent().getStringExtra("partid");
        coursecode = getIntent().getStringExtra("coursecode");
        boolean isloaded = getIntent().getBooleanExtra("isloaded",false);
        isLoading = true;
        initView();
        chapterHandler.sendEmptyMessageDelayed(1000,1000);
    }

    private int getSlideTime(){
        return (int)Math.ceil(slideCnt*0.5)*10;
    }

    private void initView(){
        llLoading = findViewById(R.id.ll_loading);
        icLoading = findViewById(R.id.ic_loading);
        llChapion = findViewById(R.id.ll_champion);
        llTest = findViewById(R.id.ll_test);

        tmvLoading = (TalkMateLoadingView) findViewById(R.id.tmv_loading);
        tvLoading = (TextView) findViewById(R.id.tv_loading);
        imgAvatar = (ImageView)findViewById(R.id.img_avatar);
        tvRank = (TextView)findViewById(R.id.tv_rank);
        tvName = (TextView)findViewById(R.id.tv_name);
        tvInfo = (TextView)findViewById(R.id.tv_info);
        tvTestCnt = (TextView) findViewById(R.id.tv_test_cnt);
        tvTime = (TextView)findViewById(R.id.tv_time);

        TextView tvCourseName = (TextView)findViewById(R.id.tv_course_name);

        tvCourseName.setText("Hello FuckMates");
        TextView tvStart = (TextView) findViewById(R.id.tv_start);
        tvStart.getBackground().setColorFilter(0xff54a3fc, PorterDuff.Mode.SRC_IN);
        tvStart.setOnClickListener(this);
        tvHistory = (TextView) findViewById(R.id.tv_history);
        tvHistory.getBackground().setColorFilter(0xff54a3fc, PorterDuff.Mode.SRC_IN);
        tvHistory.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isLoading) {
            chapterHandler.sendEmptyMessage(LOADING);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        chapterHandler.removeCallbacksAndMessages(null);
    }

    private void getLearnMates(){
        showLearnMateData();
    }

    private void showTestView() {
        AnimationSet blood1 = new AnimationSet(true);
        TranslateAnimation speo = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.1f);
        AlphaAnimation sbeo = new AlphaAnimation(1f,0f);
        blood1.setDuration(200);
        blood1.addAnimation(sbeo);
        blood1.addAnimation(speo);
        blood1.setFillAfter(true);
        blood1.setInterpolator(new AccelerateInterpolator());
        llLoading.startAnimation(blood1);
        chapterHandler.sendEmptyMessageDelayed(HIDE_LOADING,800);
//        llLoading.setVisibility(View.GONE);

        tvHistory.setVisibility(hasHistory?View.VISIBLE:View.GONE);
        llTest.setVisibility(View.VISIBLE);
        int time = getSlideTime();
        tvTime.setText(String.format("%1$02d:%2$02d",time/60,time%60));
        tvTestCnt.setText(String.format(getString(R.string.testers_num),testCnt+""));

        AnimationSet blood2 = new AnimationSet(true);
        TranslateAnimation sbm = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
                Animation.RELATIVE_TO_SELF,0.3f,Animation.RELATIVE_TO_SELF,0f);
        AlphaAnimation spm = new AlphaAnimation(0.5f,1f);
        blood2.setDuration(800);
        blood2.addAnimation(sbm);
        blood2.addAnimation(spm);
        blood2.setInterpolator(new AccelerateInterpolator());
        llTest.startAnimation(blood2);
    }

    private void showLearnMateData() {
        if(!isFinishing()){
            tvName.setText("SBO");
            String blood = "Hello Fuck You Bitch";
            tvInfo.setText(blood);

            llLoading.clearAnimation();
            int height = (llLoading.getHeight()-icLoading.getHeight())/2;

            TranslateAnimation loadingTrans = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_SELF,0f);
            loadingTrans.setDuration(400);
            loadingTrans.setInterpolator(new DecelerateInterpolator());

            TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_SELF,0f);
            AlphaAnimation alpha = new AlphaAnimation(0f,1f);
            final AnimationSet set = new AnimationSet(true);
            set.setDuration(400);
//            set.addAnimation(trans);
            set.addAnimation(alpha);
            set.setInterpolator(new LinearInterpolator());
            llChapion.setVisibility(View.VISIBLE);
            icLoading.startAnimation(loadingTrans);
            llChapion.startAnimation(set);

            testCnt = 10000;
        }
    }

    private void getSlideCnt(){
        chapterHandler.sendEmptyMessageDelayed(LOADED,500);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_start:
                startActivity(new Intent(this,AnimMovementTestActivity.class));
                finish();
                break;
            case R.id.tv_history:
                break;
        }
    }
}
